<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    // 2 后端 callbackName(数据)
    function onResponse(posts) {
      console.log(posts);
    }
    function show(data) {
      console.log("执行show", data);
    }
    function jsonp({ url, params, cb }) {
      return new Promise((resovle, reject) => {
        window[cb] = function (data) {
          resovle(data);
          document.body.removeChild(script);
        };
        let arrs = [];
        let script = document.createElement("script");
        params = { ...params, cb };
        console.log(params);
        for (const key in params) {
          arrs.push(`${key}=${params[key]}`);
        }
        console.log(params);
        script.src = `${url}?${arrs.join("&")}`;
        document.body.appendChild(script);
      });
    }

    jsonp({
      url: "http://localhost:9000/say",
      params: { wd: "我爱中国" },
      cb: "show",
    }).then((data) => {
      console.log(data);
    });
  </script>
  <!-- <script src="http://localhost:9090/api"></script> -->
</html>
